package com.example.kingdeescan.adapter;

import android.content.Context;
import android.util.SparseBooleanArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.kingdeescan.R;
import com.example.kingdeescan.dto.ScanResultDTO;

import java.util.List;

import androidx.appcompat.widget.AppCompatCheckBox;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * author  : zly
 * e-mail  : 2444639551@qq.com
 * date    : 2020/7/9 11:23
 * alter   : zly
 * version : 1.0
 * desc    : 扫码结果适配器优化版
 * address : Kingdee in Zhangzhou
 */
public class ScanResultNewAdapter extends BaseAdapter {

    //存储扫码列表(用LinkedList实现，增删效率高)
    private List<ScanResultDTO> scanResultDTOList;
    private Context mContext;
    //ViewHolder通常出现在适配器里，为的是ListView滚动的时候快速设置值，而不必每次都重新创建很多对象，从而提升性能
    ViewHolderInner viewHolderInner;
    private int resourceId;
    private boolean isShowCheckBox=false;
    //用来存放CheckBox的选中状态，true为选中,false为没有选中
    private SparseBooleanArray stateCheckedMap;

    /**
     * 传递上下文环境、数据集合、checkbox选中状态到适配器
     * @param context
     * @param dataList
     * @param stateCheckedMap
     */
    public ScanResultNewAdapter(Context context, int resource, List<ScanResultDTO> dataList, SparseBooleanArray stateCheckedMap) {
        this.mContext=context;
        this.resourceId=resource;
        this.scanResultDTOList =dataList;
        this.stateCheckedMap=stateCheckedMap;
    }

    @Override
    public int getCount() {
        return scanResultDTOList.size();
    }

    @Override
    public Object getItem(int position) {
        return scanResultDTOList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    /**
     * 重写获取视图，优化listview
     * @param position
     * @param convertView
     *      用于将之前加载好的布局进行缓存，便于重复利用
     *      如果convertView为null，则使用LayoutInflater去加载布局，不为空则直接重用convertView。
     * @param parent
     * @return View
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ScanResultDTO scanResultDTO = (ScanResultDTO) getItem(position);//获取当前项的实例

        //如果convertView不为空，重用convertView
        if (null!=convertView){
            view=convertView;
            //重新获取ViewHolder（利用View的getTag()方法，把ViewHolder重新取出）
            viewHolderInner=(ViewHolderInner)view.getTag();
        }else{//如果convertView为空，则使用LayoutInflater()去加载布局
            //LayoutInflater的inflate()方法接收3个参数：需要实例化布局资源的id、ViewGroup类型视图组对象、false
            //false表示只让父布局中声明的layout属性生效，但不会为这个view添加父布局
            view= LayoutInflater.from(mContext).inflate(resourceId,parent,false);
            viewHolderInner=new ScanResultNewAdapter.ViewHolderInner(view);
            //通过ViewHolder获取实例
            viewHolderInner.tvWldm=view.findViewById(R.id.tv_wldm);
            viewHolderInner.tvQuantity=view.findViewById(R.id.tv_quantity);
            viewHolderInner.checkBox=view.findViewById(R.id.chb_select_way_point);
            //将ViewHolder存储在view中
            view.setTag(viewHolderInner);
        }
        //设置文字（物料代码和数量）
        viewHolderInner.tvWldm.setText(scanResultDTO.getWldm());
        viewHolderInner.tvQuantity.setText(String.valueOf(scanResultDTO.getQuantity()));
        //显示或隐藏CheckBox
        showAndHideCheckBox();
        //设置checkbox的勾选状态
        viewHolderInner.checkBox.setChecked(stateCheckedMap.get(position));
        //返回view
        return view;
    }

    /**
     * 显示和隐藏checkbox
     */
    private void showAndHideCheckBox() {
        if (isShowCheckBox) {
            viewHolderInner.checkBox.setVisibility(View.VISIBLE);
        } else {
            viewHolderInner.checkBox.setVisibility(View.GONE);
        }
    }

    /**
     * 设置checkbox
     * @param showCheckBox
     */
    public void setShowCheckBox(boolean showCheckBox) {
        isShowCheckBox = showCheckBox;
    }

    /**
     * 定义ViewHolderInner内部类，用于对控件实例进行缓存
     */
    public class ViewHolderInner{
        @BindView(R.id.tv_wldm)
        public TextView tvWldm;
        @BindView(R.id.tv_quantity)
        public TextView tvQuantity;
        @BindView(R.id.chb_select_way_point)
        public AppCompatCheckBox checkBox;

        public ViewHolderInner(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
